package com.neusoft.elmboot.controller;

import com.neusoft.elmboot.po.Business;
import com.neusoft.elmboot.po.BusinessDTO;
import com.neusoft.elmboot.po.User;
import com.neusoft.elmboot.service.BusinessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/BusinessController")
public class BusinessController {

    @Autowired
    private BusinessService businessService;

    @GetMapping ("/listBusinessByOrderTypeId/{orderTypeId}")
    public Map<String, Object> listBusinessByOrderTypeId(@PathVariable("orderTypeId") Integer orderTypeId) throws Exception {
        List<Business> businessList = businessService.listBusinessByOrderTypeId(orderTypeId);

        List<BusinessDTO> businessDTOList = new ArrayList<>();
        for (Business business : businessList) {
            BusinessDTO businessDTO = new BusinessDTO();
            businessDTO.setBusinessId(business.getBusinessId());
            businessDTO.setBusinessName(business.getBusinessName());
            businessDTO.setBusinessImg(business.getBusinessImg());
            businessDTO.setDeliveryPrice(business.getDeliveryPrice());
            businessDTO.setStarPrice(business.getStarPrice());
            businessDTO.setBusinessExplain(business.getBusinessExplain());
            businessDTOList.add(businessDTO);

        }
        Map<String, Object> response = new HashMap<>();
        response.put("result", businessDTOList); // 将数据封装到 result 字段中
        return response;
    }

    @GetMapping("/listBusinessByKeyWords/{businessName}")
    public Map<String, Object> listBusinessByKeyWords(@PathVariable("businessName") String businessName) throws Exception {
        List<Business> businessList = businessService.listBusinessByKeyWords(businessName);

        List<BusinessDTO> businessDTOList = new ArrayList<>();
        for (Business business : businessList) {
            BusinessDTO businessDTO = new BusinessDTO();
            businessDTO.setBusinessId(business.getBusinessId());
            businessDTO.setBusinessName(business.getBusinessName());
            businessDTO.setBusinessImg(business.getBusinessImg());
            businessDTO.setDeliveryPrice(business.getDeliveryPrice());
            businessDTO.setStarPrice(business.getStarPrice());
            businessDTO.setBusinessExplain(business.getBusinessExplain());
            businessDTOList.add(businessDTO);
        }
        Map<String, Object> response = new HashMap<>();
        response.put("result", businessDTOList);
        return response;
    }

    @GetMapping("/getBusinessById/{businessId}")
    public Map<String, Object> getBusinessById(@PathVariable("businessId") Integer businessId) throws Exception {
        // 调用业务层方法获取 Business 对象
        Business business = businessService.getBusinessById(businessId);

        if (business == null) {
            Map<String, Object> errorResponse = new HashMap<>();
            errorResponse.put("message", "Business not found for ID: " + businessId);
            return errorResponse;
        }

        // 将 Business 对象转换为 BusinessDTO 对象
        BusinessDTO businessDTO = new BusinessDTO();
        businessDTO.setBusinessId(business.getBusinessId());
        businessDTO.setBusinessName(business.getBusinessName());
        businessDTO.setBusinessImg(business.getBusinessImg());
        businessDTO.setDeliveryPrice(business.getDeliveryPrice());
        businessDTO.setStarPrice(business.getStarPrice());
        businessDTO.setBusinessExplain(business.getBusinessExplain());

        // 创建返回结果的 Map 对象
        Map<String, Object> response = new HashMap<>();

        // 将 Business 对象封装到 result 字段中
        response.put("result", businessDTO);

        // 返回封装好的响应
        return response;
    }

    @PostMapping("/saveBusiness")
    public Map<String, Object> saveBusiness(@RequestBody Business business) {
        System.out.println("Received Business: " + business);  // 打印接收的 business 对象

        Map<String, Object> response = new HashMap<>();
        try {
            int result = businessService.saveBusiness(business);
            if (result > 0) {
                response.put("success", true);
            } else {
                response.put("success", false);
            }
        } catch (Exception e) {
            response.put("success", false);
            response.put("error", e.getMessage());
            e.printStackTrace();  // 打印异常信息
        }
        return response;
    }
}
